package com.example.botany_api.controller;

import com.alibaba.fastjson.JSON;
import com.example.botany_api.bean.Carousel;
import com.example.botany_api.bean.User;
import com.example.botany_api.service.SystemService;
import com.example.botany_api.service.UserService;
import com.example.botany_api.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author 漫画忧作
 * @Date 2022/10/29 23:24
 * @Version 1.0
 */
@RestController
public class SystemController {
    @Autowired
    private SystemService systemService;

    //    管理员 获取轮播图分页数据接口
    @RequestMapping("/admin/getCarouselImgList")
    public String getAdminList(@RequestBody HashMap req) {
        System.out.println(req.get("time"));
        String time = req.get("time").toString();
        int current = Integer.parseInt(req.get("current").toString());
        int size = Integer.parseInt(req.get("size").toString());
        Map res = new HashMap();
        res.put("dataList", systemService.getCarouselImgList(time, (current - 1) * size, size));
        res.put("total", systemService.getCarouselImgTotal(time));

        return JSON.toJSONString(res);
    }
    //    获取轮播图数据接口
    @RequestMapping("/user/getCarouselImgList")
    public String getUserList() {
        Map res = new HashMap();
        res.put("dataList", systemService.getCarouselImgList("", 0, 10));
        return JSON.toJSONString(res);
    }
    @Autowired
    OssUtil ossUtil;
    String imageUrl=null;
    //存储图片地址
    @RequestMapping("/admin/setCarouselImg")
    public void addImgHead(MultipartFile file){
        //调用上传阿里云oss
        imageUrl = ossUtil.checkImage(file);  //image 为MultipartFile类型
        System.out.println(imageUrl);
    }

    //添加轮播图数据
    @RequestMapping("/admin/add_CarouselImg")
    public String add_CarouselImg(@RequestBody Carousel carousel){
        if(imageUrl==null){
            return "img";
        }
        carousel.setImg_url(imageUrl);
        carousel.setTime(carousel.getTime());
        int i=systemService.add_CarouselImg(carousel);
        imageUrl=null;
        if(i==1){
            return "ok";
        }

        return "err";
    }
    //    管理员 删除轮播图
    @RequestMapping("/admin/dl_CarouselImg")
    public String dl_CarouselImg(@RequestBody HashMap req) {
        int id = Integer.parseInt(req.get("id").toString());
        int i = systemService.dl_CarouselImg(id);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }
    //    管理员 获得尾部数据
    @RequestMapping("/get_tailData")
    public String get_tailData() {
        Map res=new HashMap();
        res.put("dataList",systemService.get_tailData());

        return JSON.toJSONString(res);
    }
    //    管理员 修改尾部数据
    @RequestMapping("/admin/up_tailData")
    public String up_tailData(@RequestBody HashMap req) {
        String data_one = req.get("data_one").toString();
        String data_two = req.get("data_two").toString();
        String data_three = req.get("data_three").toString();
        int i=systemService.up_tailData(data_one,data_two,data_three);
        if (i == 1) {
            return "ok";
        }
        return "err";
    }
}
